package com.web.cms.pan.util;

import com.blue.core.util.NumberUtil;
import com.blue.integration.serve.Bucket;
import com.blue.integration.serve.StorageClient;
import com.blue.jdbc.Page;
import com.web.cms.pan.message.DeleteFileDataRequest;
import com.web.cms.pan.message.DeleteFileDataResponse;
import com.web.cms.pan.model.DeleteFileData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 待删除文件数据转换器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年04月09日
 */
public class DeleteFileDataConvertor
{
	private DeleteFileDataConvertor()
	{
	}
	
	public static DeleteFileData toDeleteFileData(DeleteFileDataRequest request)
	{
		if (request == null)
			return null;
		
		DeleteFileData deleteFileData = new DeleteFileData();
		deleteFileData.setId(request.getId());
		deleteFileData.setUserId(request.getUserId());
		deleteFileData.setName(request.getName());
		deleteFileData.setKey(request.getKey());
		deleteFileData.setMimeType(request.getMimeType());
		deleteFileData.setCreateTime(request.getCreateTime());
		deleteFileData.setDeleteTime(request.getDeleteTime());
		deleteFileData.setSize(request.getSize());
		deleteFileData.setType(request.getType());
		deleteFileData.setState(request.getState());
		return deleteFileData;
	}
	
	public static List<DeleteFileData> toDeleteFileData(List<DeleteFileDataRequest> requestList)
	{
		List<DeleteFileData> list = new ArrayList<>();
		if (requestList == null || requestList.isEmpty())
			return list;
		
		for (DeleteFileDataRequest request : requestList)
		{
			list.add(toDeleteFileData(request));
		}
		return list;
	}
	
	public static DeleteFileDataResponse toResponse(DeleteFileData deleteFileData, StorageClient storageClient)
	{
		if (deleteFileData == null)
			return null;
		
		DeleteFileDataResponse response = new DeleteFileDataResponse();
		response.setId(deleteFileData.getId());
		response.setUserId(deleteFileData.getUserId());
		response.setName(deleteFileData.getName());
		response.setKey(deleteFileData.getKey());
		response.setMimeType(deleteFileData.getMimeType());
		response.setCreateTime(deleteFileData.getCreateTime());
		response.setDeleteTime(deleteFileData.getDeleteTime());
		response.setSize(deleteFileData.getSize());
		response.setType(deleteFileData.getType());
		response.setState(deleteFileData.getState());

		response.setSizeName(NumberUtil.byteFormat(deleteFileData.getSize().intValue()));
		response.setUrl(storageClient.getUrl(Bucket.PAN, deleteFileData.getKey()));
		return response;
	}
	
	public static List<DeleteFileDataResponse> toResponse(List<DeleteFileData> list, StorageClient storageClient)
	{
		List<DeleteFileDataResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (DeleteFileData deleteFileData : list)
		{
			responseList.add(toResponse(deleteFileData, storageClient));
		}
		return responseList;
	}
	
	public static Map<Integer, DeleteFileDataResponse> toResponse(Map<Integer, DeleteFileData> map, StorageClient storageClient)
	{
		Map<Integer, DeleteFileDataResponse> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;
		
		for (Map.Entry<Integer, DeleteFileData> entry : map.entrySet())
		{
			responseMap.put(entry.getKey(), toResponse(entry.getValue(), storageClient));
		}
		return responseMap;
	}
	
	public static Page<DeleteFileDataResponse> toResponse(Page<DeleteFileData> page, StorageClient storageClient)
	{
		if (page == null)
			return null;
		
		Page<DeleteFileDataResponse> responsePage = new Page<>();
		responsePage.setCurPage(page.getCurPage());
		responsePage.setRows(page.getRows());
		responsePage.setSize(page.getSize());
		responsePage.setStartRowNo(page.getStartRowNo());
		responsePage.setTotalPage(page.getTotalPage());
		responsePage.setObjectList(toResponse(page.getObjectList(), storageClient));
		return responsePage;
	}
	
}
